package leetcode;

import java.util.Arrays;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;

/**
 * ClassName: ZhonganT1
 * Package: leetcode
 * Description:
 *
 * @Author wjn
 * @Create 2025/9/12 19:27
 * @Version 1.0
 */
public class ZhonganT1 {

    static class House implements Comparable<House>{
        int price;
        int comfort;

        public House(int price, int comfort) {
            this.price = price;
            this.comfort = comfort;
        }

        @Override
        public int compareTo(House o) {
            if (this.price != o.price) {
                return Integer.compare(this.price, o.price);
            }
            return Integer.compare(o.comfort, this.comfort);
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();

        int[] friends = new int[n];
        for (int i = 0; i < n; i++) {
            friends[i] = scanner.nextInt();
        }

        House[] houses = new House[m];
        for (int i = 0; i < m; i++) {
            int comfort = scanner.nextInt();
            int price = scanner.nextInt();
            houses[i] = new House(price, comfort);
        }
        scanner.close();

        Arrays.sort(houses);
        Arrays.sort(friends);

        PriorityQueue<Integer> availableComforts = new PriorityQueue<>(Collections.reverseOrder());

        long totalComfort = 0;
        int houseIndex = 0;

        for(int gold : friends){
            while (houseIndex < m && houses[houseIndex].price <= gold) {
                availableComforts.add(houses[houseIndex].comfort);
                houseIndex++;
            }

            if (!availableComforts.isEmpty()) {
                totalComfort += availableComforts.poll();
            }
        }
        System.out.println(totalComfort);

    }
}
